using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using RBAC.Api.Demain.ContentManagement;
using RBAC.Api.Erroc;
using RBAC.Api.Read.Command.ContentManagement;
using RBAC.Api.Read.Dto.HospitalDetails;

namespace RBAC.Api.Read.Controllers.ContentManagement
{
    /// <summary>
    /// 医院管理控制器--读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HospitalController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;

        /// <summary>
        /// 日志记录器
        /// </summary>
        private readonly ILogger<HospitalController> logger;

        public HospitalController(IMediator mediator, ILogger<HospitalController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 获取医院列表
        /// </summary>
        /// <param name="name">医院名称</param>
        /// <param name="address">医院地址</param>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageSize">条数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<APIPagin<HospitalModel>>> ShowHospital(string? name, string? address, int pageIndex = 1, int pageSize = 10)
        {
            try
            {
                logger.LogInformation("开始获取医院列表，查询参数：名称={Name}, 地址={Address}, 页数={PageIndex}, 条数={PageSize}", 
                    name, address, pageIndex, pageSize);

                ShowHospitalCommand command = new ShowHospitalCommand();
                command.Name = name;
                command.Address = address;
                command.PageIndex = pageIndex;
                command.PageSize = pageSize;

                var result = await mediator.Send(command);
                
                logger.LogInformation("获取医院列表成功，返回数据条数：{Count}", result.Data?.List?.Count ?? 0);
                
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "获取医院列表失败，查询参数：名称={Name}, 地址={Address}, 页数={PageIndex}, 条数={PageSize}", 
                    name, address, pageIndex, pageSize);
                throw;
            }
        }
        /// <summary>
        /// 获取医院详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<HospitalDetailsDto>>> ShowHospitalDetails(long departmentsId)
        {
            ShowHospitalDetailsCommand command = new ShowHospitalDetailsCommand();
            command.DepartmentsId = departmentsId;
            return mediator.Send(command);
        }
    }
}